#include "FormatVideo.h"
#include <set>
namespace Media{

const int bitrate_video_max = 8 * 1024; //8*1024*1024
const int bitrate_video_min = 128;	//128*1024

const float fps_max = 60.0;
const float fps_min = 10.0;

const int gop_max = 300;
const int gop_min = 1;

const int quality_max = 6;
const int quality_min = 1;

struct resolutionk{
	int width = 0;
	int height = 0;
	bool operator <(const resolutionk &other) const{
		if(width < other.width){
			return true;
		}
		if(height < other.height){
			return true;
		}
		return false;
	}
	resolutionk() = default;
	resolutionk(int w, int h):width(w), height(h){}
};

struct FormatHelper{


	FormatHelper(){
		_compress_map["MPEG4"] = videoEncMPEG4;
		_compress_map["MPEG2"] = videoEncMPEG2;
		_compress_map["MJPG"] = videoEncMotionJPEG;
		_compress_map["H264"] = videoEncH264;
		_compress_map["H265"] = videoEncH265;

		_brc_map["CBR"] = videoEncBitrateCtrlConstant;
		_brc_map["VBR"] = videoEncBitrateCtrlVariable;

		_profile_map["BaseLine"] = h264ProfileBaseline;
		_profile_map["Main"] = h264ProfileMain;
		_profile_map["Extended"] = h264ProfileExtended;
		_profile_map["High"] = h264ProfileHigh;

		_resolution_set.insert(resolutionk(2560, 1440));
		_resolution_set.insert(resolutionk(1920, 1080));
		_resolution_set.insert(resolutionk(1280, 720));
		_resolution_set.insert(resolutionk(720, 480));
		_resolution_set.insert(resolutionk(480, 360));
	}
	~FormatHelper() = default;
	std::map<std::string, VideoEncType>			_compress_map;
	std::map<std::string, VideoEncBitrateCtrl>	_brc_map;
	std::map<std::string, VideoEncH264Profile>	_profile_map;
	std::set<resolutionk>						_resolution_set;
};

static FormatHelper s_FormatHelper;

bool FormatVideo::EncType(const std::string &name, VideoEncType &type){
	if(s_FormatHelper._compress_map.find(name) != s_FormatHelper._compress_map.end()){
		type = s_FormatHelper._compress_map[name];
		return true;
	}
	return false;
}

bool FormatVideo::BrcType(const std::string &name, VideoEncBitrateCtrl &type){
	if(s_FormatHelper._brc_map.find(name) != s_FormatHelper._brc_map.end()){
		type = s_FormatHelper._brc_map[name];
		return true;
	}
	return false;
}

bool FormatVideo::Profile(const std::string &name, VideoEncH264Profile &type){
	if(s_FormatHelper._profile_map.find(name) != s_FormatHelper._profile_map.end()){
		type = s_FormatHelper._profile_map[name];
		return true;
	}
	return false;
}


bool FormatVideo::verifyBitrate(int bitrate){
	return (bitrate >= bitrate_video_min && bitrate <= bitrate_video_max);
}

bool FormatVideo::verifyCompression(const std::string &compression){
	return s_FormatHelper._compress_map.find(compression) != s_FormatHelper._compress_map.end();
}

bool FormatVideo::verifyBrc(const std::string &brc){
	return s_FormatHelper._brc_map.find(brc) != s_FormatHelper._brc_map.end();
}

bool FormatVideo::verifyResolution(int width, int height){
	return s_FormatHelper._resolution_set.find(resolutionk(width, height)) != s_FormatHelper._resolution_set.end();
}

bool FormatVideo::verifyFps(float fps){
	return (fps >= fps_min && fps <= fps_max);
}

bool FormatVideo::verifyGop(int gop){
	return (gop >= gop_min && gop <= gop_max);
}

bool FormatVideo::verifyProfile(const std::string profile){
	return s_FormatHelper._profile_map.find(profile) != s_FormatHelper._profile_map.end();
}

bool FormatVideo::verifyQuality(int quality){
	return (quality >= quality_min && quality <= quality_max);
}


}


